package com.yfbao.horizon.compress;

import org.msgpack.MessagePack;
import org.msgpack.MessageTypeException;
import org.msgpack.packer.Packer;
import org.msgpack.type.Value;
import org.msgpack.unpacker.Unpacker;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * TODO: 暂时还有问题
 */
public class MessagePackSerizlization {
    private static final MessagePack messagePack = new MessagePack();
    static {
        init();
    }

    public static byte[] serialize(Object obj) {
        try {
//            ByteArrayOutputStream baos = new ByteArrayOutputStream();
//            Packer packer = messagePack.createPacker(baos);
//            packer.write(obj);
//            packer.flush();
//            return baos.toByteArray();
            return messagePack.write(obj);
        }catch (IOException e){

        }
        return null;

    }

    public static <T> T deserialize(byte[] data,Class<T> aClazz)  {
        try {
//            ByteArrayInputStream bais = new ByteArrayInputStream(data);
//            Unpacker unpacker = messagePack.createUnpacker(bais);
//            return unpacker.read(aClazz);
            MessagePack messagePack = new MessagePack();
            return messagePack.read(data,aClazz);
        }catch (IOException | MessageTypeException e){
        }
        return null;
    }

    private static void init(){
        messagePack.register(CtpEntityResource.class);
    }
}
